package com.ls.socketio.handler;
import java.util.*;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.ls.socketio.bean.*;
import com.ls.socketio.repository.dao.*;
import lombok.extern.java.Log;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liShan
 * @date 2019/9/2 10:56
 */
@Component
@Log
public class MessageEventHandler {

    private ListClient listClient = ListClient.getInstance();
    @Resource
    private RoomRepositoryImpl roomRepositoryImpl;

    @Resource
    private RoomBoxRepositoryImpl roomBoxRepositoryImpl;

    @Resource
    private RoomEnvRepositoryImpl roomEnvRepositoryImpl;

    @Resource
    private RoomAirConditionerRepositoryImpl roomAirConditionerRepositoryImpl;

    @Resource
    private RoomBaseRepositoryImpl roomBaseRepositoryImpl;

    @Resource
    private RoomLampRepositoryImpl roomLampRepositoryImpl;

    @OnConnect
    public void onConnect(SocketIOClient client) {
        listClient.getListClients().add(client.getSessionId());
        log.info("客户端:" + client.getSessionId() + "已连接");
    }

    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {
        log.info("客户端:"+client.getSessionId() + "已断开");
        listClient.getListClients().remove(client.getSessionId());
        listClient.getClientRoom().remove(client.getSessionId().toString());
    }


    @OnEvent(value = "messageevent")
    public void onEvent(SocketIOClient client, AckRequest request, String data) {
        log.info("发来消息：" + data);
    }

    @OnEvent(value = "selectRooms")
    public void selectRooms(SocketIOClient client, AckRequest request, String data) {
        log.info("发来消息：" + data);
        // 查询该楼层所有房间的，限制10个房间
        // 查询房间id
        Set<Long> roomIds = new HashSet<> ();
        List<RoomInfo> roomInfos = roomRepositoryImpl.findRoomByLayerId(Long.valueOf(data));
        Optional.ofNullable(roomInfos).ifPresent(rooms ->
                rooms.forEach(msg -> {
                    // 查询房间基本参数
                    roomIds.add(msg.getRoomId());
                    RoomBaseInfo info = roomBaseRepositoryImpl.findByRoomId(msg.getRoomId());
                    msg.setBaseInfos(Stream.of(info).collect(Collectors.toList()));
                    RoomAirConditionerInfo airInfo = roomAirConditionerRepositoryImpl.findByRoomId(msg.getRoomId());
                    msg.setAirInfos(Stream.of(airInfo).collect(Collectors.toList()));
                    RoomBoxInfo roomBoxInfo = roomBoxRepositoryImpl.findByRoomId(msg.getRoomId());
                    msg.setRoomBoxInfos(Stream.of(roomBoxInfo).collect(Collectors.toList()));
                    RoomEnvInfo envInfo = roomEnvRepositoryImpl.findByRoomId(msg.getRoomId());
                    msg.setEnvInfos(Stream.of(envInfo).collect(Collectors.toList()));
                    RoomLampInfo lampInfo = roomLampRepositoryImpl.findByRoomId(msg.getRoomId());
                    msg.setLampInfos(Stream.of(lampInfo).collect(Collectors.toList()));
                })
        );
        listClient.getClientRoom().put(client.getSessionId().toString(), roomIds);
        client.sendEvent("enewbuys", roomInfos);
    }

}
